// 定义 HttpService 类
function HttpService(baseUrl) {
    this.baseUrl = baseUrl;
}

HttpService.prototype.request = async function (url, method = 'GET', body = null, headers = {}) {
    const config = {
        method,
        url: `${this.baseUrl}${url}`,
    };

    if (body) {
        config.data = body;
    }

    if (Object.keys(headers).length > 0) {
        config.headers = headers;
    }

    try {
        const response = await axios(config);
        return response.data;
    } catch (error) {
        console.error('Request failed:', error);
        if (error.response) {
            throw new Error(`HTTP error! status: ${error.response.status}`);
        } else {
            throw error;
        }
    }
};

HttpService.prototype.get = function (url, headers = {}) {
    return this.request(url, 'GET', null, headers);
};

HttpService.prototype.post = function (url, body, headers = {}) {
    return this.request(url, 'POST', body, headers);
};

HttpService.prototype.put = function (url, body, headers = {}) {
    return this.request(url, 'PUT', body, headers);
};
HttpService.prototype.delete = function (url, body, headers = {}) {
    return this.request(url, 'DELETE', body, headers);
};

// 将 HttpService 暴露为全局变量
window.HttpService = HttpService;